﻿using System;
using System.ComponentModel;
using Zinnia.Common.Core;
using Zinnia.Common.Core.Imp;

namespace Zinnia.Common.Untilty
{
    /// <summary>
    /// 本级缓存帮助类
    /// </summary>
    public sealed class LocalCacheHelper
    {
        IDependencyCacheService cache;
        private LocalCacheHelper()
        {
            cache = new LocalDependencyCacheService();
        }

        public static LocalCacheHelper Instance => new LocalCacheHelper();

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        public void Add<T>(T obj, string key)
        {
            cache.Add(obj, key);
        }

        /// <summary>
        /// 添加缓存带有缓存依赖
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <param name="cacheDependencyKey">依赖键</param>
        public void Add<T>(T obj, string key, string cacheDependencyKey)
        {
            cache.Add(obj, key, cacheDependencyKey);
        }

        /// <summary>
        /// 添加缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <param name="key"></param>
        /// <param name="expireSeconds"></param>
        public void Add<T>(T obj, string key, int expireSeconds)
        {
            cache.Add(obj, key, expireSeconds);
        }

        /// <summary>
        /// 获取缓存值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public T GetCacheOjbect<T>(string key)
        {
            try
            {
                return cache.GetCacheOjbect<T>(key);
            }
            catch
            {
                return default(T);
            }
        }

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key"></param>
        public void RemoveCache(params string[] key)
        {
            cache.RemoveCache(key);
        }
    }
}
